package com.xinheyun.demo.leetcode;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * @description: TODO
 * @author: Sarah
 * @modified By: Sarah
 * @date: Created in 2021/8/8 15:42
 * @version:v1.0
 */
public class test102 {

}

//class TreeNode {
//    int val;
//    TreeNode left;
//    TreeNode right;
//    TreeNode() {}
//    TreeNode(int val) { this.val = val; }
//    TreeNode(int val, TreeNode left, TreeNode right) {
//        this.val = val;
//        this.left = left;
//        this.right = right;
//    }
//}

class Solution {

    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> list = new ArrayList<>();
        Deque<TreeNode> queue = new ArrayDeque<>();
        if(root == null)
            return list;
        queue.offer(root);
        while(!queue.isEmpty()){
            List<Integer> listIn = new ArrayList<>();
            int size = queue.size();
            for(int i=0;i<size;i++) {

                TreeNode treeNode = queue.poll();
                if (treeNode.left != null) {
                    queue.offer(treeNode.left);
                }
                if (treeNode.right != null) {
                    queue.offer((treeNode.right));
                }
                listIn.add(treeNode.val);
            }
            list.add(listIn);
        }
        return list;





    }
}